package com.bsh.controller.system;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bsh.mybatis.model.Role;
import com.bsh.service.ModuleService;
import com.bsh.service.RoleService;
import com.bsh.utils.PageJosnMessage;
import com.bsh.utils.PageTools;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * @author gsh
 * @since 0.0.1
 * 
 */
@Controller
public class RoleController {
	private static Logger log = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	HttpServletRequest request;
    @Autowired
    HttpServletResponse response;
    @Autowired
    HttpSession session;    
    @Autowired
    ModuleService module;
	@Autowired
	private RoleService roleService;

	/**
	 * 角色列表
	 */
	@RequestMapping(value = "/system/role/list")
	public ModelAndView roleList() {
		String roleName = request.getParameter("roleName") == null ? null : request.getParameter("roleName").toString();
		String email = request.getParameter("email") == null ? null : request.getParameter("email").toString();

		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/roleList");

		String pageStr = request.getParameter("pageIndex");
		Integer pageIndex = Integer.parseInt(StringUtils.isNullOrEmpty(pageStr) ? "1" : pageStr);

		String sizeStr = request.getParameter("pageSize");
		Integer pageSize = Integer.parseInt(StringUtils.isNullOrEmpty(sizeStr) ? "10" : sizeStr);

		Pager pager = new Pager(pageIndex, pageSize);
		List<Map<String, Object>> list = roleService.getPage(pager, roleName, email);
		mv.addObject("list", list);
		mv.addObject("pager", pager);
		return mv;
	}
	

	/**
	 * 修改角色--初使化
	 */
	@RequestMapping(value = "/system/role/edit/{id}")
	public ModelAndView edit(@PathVariable(value = "id") Integer roleid) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/roleEdit");
		Role role = roleService.getRole(roleid);
		mv.addObject("role", role);
		return mv;
	}

	/**
	 * 修改角色--保存
	 */
	@RequestMapping(value = "/system/role/edit/save")
	public ModelAndView editSave(@RequestParam String name,@RequestParam short sort,@RequestParam int id,@RequestParam short isEnable) {		
		Role role = new Role();
		role.setName(name);
		role.setSort(sort);
		role.setId(id);
		role.setIsEnable(isEnable);
		
        Integer res = roleService.editRole(role);
        
        ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/roleEdit");
		mv.addObject("role", role);
		
		String msg=PageTools.alertCloseFraRefresh("修改角色","保存成功！",1000);
		mv.addObject("actionMsg",msg);
		return mv;
	}

	/**
	 * 添加角色--初使化
	 */
	@RequestMapping(value = "/system/role/add")
	public ModelAndView add() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/pages/system/roleAdd");
		return mv;
	}

	/**
	 * 添加角色--保存
	 */
	@RequestMapping(value = "/system/role/add/save")
	public ModelAndView addSave(@RequestParam String name,@RequestParam(defaultValue="0") short sort,@RequestParam(defaultValue="1") short isEnable) {
		ModelAndView mv = new ModelAndView();
		Role role = new Role();
		role.setName(name);
		role.setSort(sort);
		role.setIsEnable(isEnable);
		role.setCreateTime(new Date());
		
		
		Integer res = roleService.addRole(role);
		mv.setViewName("/pages/system/roleAdd");
	       
		String msg=PageTools.alertCloseFraRefresh("添加角色","保存成功！",1000);
		mv.addObject("actionMsg",msg);
		return mv;
	}


	/**
	 * 查看角色信息
	 */
	@ResponseBody
	@RequestMapping(value = "/system/role/view/{id}")
	public PageJosnMessage view(@PathVariable(value = "id") Integer roleid) {
        Role role =  roleService.getRole(roleid);
        PageJosnMessage msg=new PageJosnMessage();
        msg.putData("role",role);
		return msg;
	}
	
	/**
	 * 启用、禁用角色
	 */
	@ResponseBody
	@RequestMapping(value = "/system/role/enable")
	public PageJosnMessage enable(@RequestParam int id,@RequestParam int isEnable) {
        Integer res =  roleService.enableRole(id, isEnable == 1);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:0);
		return msg;
	}
	
	/**
	 * 批量禁用
	 */
	@ResponseBody
	@RequestMapping(value = "/system/role/disable/batch")
	public PageJosnMessage disableBatch(@RequestBody int[] idList) {
        Integer res =  roleService.disableBatch(idList);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:0);
		return msg;
	}
	
	/**
	 * 批量启用
	 */
	@ResponseBody
	@RequestMapping(value = "/system/role/enable/batch")
	public PageJosnMessage enableBatch(@RequestBody int[] idList) {
        Integer res =  roleService.enableBatch(idList);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:0);
		return msg;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "/system/role/delete/{id}")
	public PageJosnMessage delete(@PathVariable(value = "id") Integer id) {
        Integer res =  roleService.deleteRole(id);
        PageJosnMessage msg=new PageJosnMessage();
        res=(res==null?1:res);
        msg.putData("status",res>0?1:0);
		return msg;
	}
	
}
